<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 比如我要过滤 "SM"
    //  var str = 'asdasdSMasdasdasdSMsdasdasdSMsadasd'
    // 需要结果
    // asdasd**asdasdasd**sdasdasd**

    var str = 'asdasdSMasdasdasdSMsdasdasdSMsadasd'
    while (str.indexOf('SM') !== -1) {
      str = str.replace("SM", "*")
    }
    console.log(str); //asdasd*asdasdasd*sdasdasd*sadasd
  </script>
</body>

</html>